package com.sunxueqi.project.system.service.impl;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sunxueqi.common.constant.StatusConstants;
import com.sunxueqi.common.utils.KeyGenerate;
import com.sunxueqi.common.utils.SecurityUtils;
import com.sunxueqi.project.system.dao.NoticeMapper;
import com.sunxueqi.project.system.service.INoticeService;
import com.sunxueqi.project.system.vo.Notice;

@Service
public class NoticeServiceImpl implements INoticeService{
	@Autowired
	private NoticeMapper NoticeMapper;
	
	@Override
	public List<Notice> selectNoticeList(Notice notice) {
		notice.setDeleteStatus(StatusConstants.NORMAL);
		return NoticeMapper.selectList(notice);
	}

	@Override
	public int add(Notice notice) {
		notice.setId(KeyGenerate.getKeyId());
		notice.setDeleteStatus(StatusConstants.NORMAL);
		notice.setCreateBy(SecurityUtils.getLoginUserId());
		notice.setCreateTime(new Timestamp(System.currentTimeMillis()));
		return NoticeMapper.insert(notice);
	}

	@Override
	public int deleteNoticeByIds(String[] idArray) {
		int result = 0;
		for(String id: idArray) {
			Notice Notice = new Notice();
			Notice.setId(id);
			Notice.setDeleteStatus(StatusConstants.DELETED);
			Notice.setDeleteTime(new Timestamp(System.currentTimeMillis()));
			Notice.setDeleteBy(SecurityUtils.getLoginUserId());
			int count = NoticeMapper.updateByPrimaryKey(Notice);
			result += count;
		}
		return result;
	}

	@Override
	public int updateNotice(Notice notice) {
		notice.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		notice.setUpdateBy(SecurityUtils.getLoginUserId());
		int count = NoticeMapper.updateByPrimaryKey(notice);
		return count;
	}

	@Override
	public int changeStatus(Notice notice) {
		return NoticeMapper.updateByPrimaryKey(notice);
	}
}
